package hu.vpszd;



import org.apache.log4j.Logger
import org.apache.log4j.spi.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Scope
import org.springframework.mail.MailException
import org.springframework.mail.MailSender
import org.springframework.mail.SimpleMailMessage
import org.springframework.stereotype.Component

@Component
@Scope("singleton")
class VpszdMailSender implements Serializable{
	private static final Logger log = Logger.getLogger(VpszdMailSender.class)

	@Autowired
	private MailSender mailSender

	void send(String to, String subject, String category, String text) {
		SimpleMailMessage msg = new SimpleMailMessage();

		msg.setFrom("vp.05@hotmail.com")
		msg.setTo(to)

		msg.setSubject(subject)
		msg.setText(text)
		try {
			MailSender mailSender = ServiceLocator.getBean(MailSender.class);
			mailSender.send(msg);
			log.info("Üzenet küldése sikeres. Üzenet:\n ${msg.to}\n${msg.subject}\n${msg.text}");
		}
		catch (MailException mex) {
			log.error("Üzenet küldése sikertelen. Üzenet:\n ${msg.from}\n${msg.to}\n${msg.subject}\n${msg.text}", mex);
			throw mex
		}
	}
}